<?php
require_once 'Config.php';
require_once 'Auth.php';
require_once 'functions.php';
require_once 'Etag.php';
require_once 'Zone.php';
require_once 'Storage\BucketManager.php';
require_once 'Storage\FormUploader.php';
require_once 'Storage\ResumeUploader.php';
require_once 'Storage\UploadManager.php';

require_once "Http/Client.php" ;
require_once "Http/Error.php" ;
require_once "Http/Request.php" ;
require_once "Http/Response.php" ;

require_once "Processing/Operation.php" ;
require_once "Processing/PersistentFop.php" ;
class Qiniu {
	private $__ACCESS_KEY = '';
    private $__SECRET_KEY = '';
    private $__BUCKET_NAME = '';
	public function __construct($config){

        $this->__ACCESS_KEY = $config['Access_Key'];
        $this->__SECRET_KEY = $config['Secret_Key'];
        $this->__BUCKET_NAME = $config['Bucket_Name'];
	
	}
	public function upload($fileName,$originName){

		// 需要填写你的 Access Key 和 Secret Key
        $accessKey = $this->__ACCESS_KEY;
        $secretKey = $this->__SECRET_KEY;

        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = $this->__BUCKET_NAME;
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 要上传文件的本地路径
        $filePath = $fileName;
        $info = pathinfo($originName);
        $ext = strtolower($info['extension']);
        $allowImg = array('jpg','jpeg','gif','png');
        if( !in_array($ext,$allowImg)){
            return ['status'=>0,'info'=>'错误的图片格式!'];
        }
        // 上传到七牛后保存的文件名
        $key = time().rand(10000,99999).'.'.$ext;

        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
      //  echo "\n====> putFile result: \n";
        if ($err !== null) {
            return ['status'=>0,'info'=>'错误的图片格式!'];

        } else {

            return ['status'=>1,'info'=>$ret];
        }
	
	}

}